//
//  XQDeviceManager.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/1/27.
//

import UIKit

class XQDeviceManager: NSObject {

    static func idfa() -> String {
        return XQCache.getIdfa();
    }
    
    static func idfv() -> String {
        if let loadData = XQKeyChainManager.load() {
            return loadData
        }else {
            let idfv = UIDevice.current.identifierForVendor?.uuidString ?? ""
            XQKeyChainManager.save(data: idfv)
            return idfv
        }
    }
    
    static func appVersion() -> String {
        return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
    }
    
    static func systemVersion() -> String {
        return UIDevice.current.systemVersion
    }
    
    static func systemName() -> String {
        return UIDevice.current.systemName
    }
    
    static func deviceName() -> String {
        return UIDevice.current.name
    }
    
    static func originalDeviceModel() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        
        return identifier
    }
    
    static func deviceModel() -> String {
       let deviceString = originalDeviceModel()
       
       switch deviceString {
       case "iPhone3,1", "iPhone3,2", "iPhone3,3":
           return "iPhone 4"
       case "iPhone4,1":
           return "iPhone 4S"
       case "iPhone5,1":
           return "iPhone 5"
       case "iPhone5,2":
           return "iPhone 5 (GSM CDMA)"
       case "iPhone5,3":
           return "iPhone 5c (GSM)"
       case "iPhone5,4":
           return "iPhone 5c (GSM CDMA)"
       case "iPhone6,1":
           return "iPhone 5s (GSM)"
       case "iPhone6,2":
           return "iPhone 5s (GSM CDMA)"
       case "iPhone7,1":
           return "iPhone 6 Plus"
       case "iPhone7,2":
           return "iPhone 6"
       case "iPhone8,1":
           return "iPhone 6s"
       case "iPhone8,2":
           return "iPhone 6s Plus"
       case "iPhone8,4":
           return "iPhone SE"
       case "iPhone9,1", "iPhone9,3":
           return "iPhone 7"
       case "iPhone9,2", "iPhone9,4":
           return "iPhone 7 Plus"
       case "iPhone10,1", "iPhone10,4":
           return "iPhone 8"
       case "iPhone10,2", "iPhone10,5":
           return "iPhone 8 Plus"
       case "iPhone10,3", "iPhone10,6":
           return "iPhone X"
       case "iPhone11,2":
           return "iPhone XS"
       case "iPhone11,4", "iPhone11,6":
           return "iPhone XS Max"
       case "iPhone11,8":
           return "iPhone XR"
       // Add more iPad models here...
       case "i386", "x86_64":
           return "Simulator"
       default:
           return deviceString
       }
   }
    
    static func isSimulator() -> Bool {
        #if targetEnvironment(simulator)
            return true
        #else
            return false
        #endif
    }
    
    static func isJury() -> Bool {
        let fileManager = FileManager.default
        let path = "/User/Applications/"

        if fileManager.fileExists(atPath: path) {
            return true
        }
        return false
    }
}
